package com.supereye.platform.web.user.controller;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.supereye.platform.common.ResultMsg;
import com.supereye.platform.util.PropUtil;
import com.supereye.platform.web.user.model.TopUser;
import com.supereye.platform.web.user.service.UserService;


@Controller
@RequestMapping("/user")
public class UserController {

	@Resource 
	private UserService userService;
	
	@RequestMapping("/getExpertsByUserAccount")
	public void getExpertsByUserAccount(HttpServletRequest request,HttpServletResponse response,String userAccount) throws Exception{
		String result = userService.getExpertsByUserAccount(userAccount);
		
		PrintWriter out = response.getWriter();  
		out.print(result);
	}
	
	@RequestMapping("/getExpertsByUserId")
	public void getExpertsByUserId(HttpServletRequest request,HttpServletResponse response,String userId) throws Exception{
		String result = userService.getExpertsByUserId(userId);
		PrintWriter out = response.getWriter();  
		out.print(result);
	}
	
	@RequestMapping("/addUser")
	@ResponseBody
	public ResultMsg addUser(HttpServletRequest request,HttpServletResponse response,TopUser user) throws Exception{
		try{
			userService.addUser(user);
			return new ResultMsg(ResultMsg.SUCCESS,"新增成功");
		}catch(Exception e){
			e.printStackTrace();
			return new ResultMsg(ResultMsg.FAIL,e.getMessage());
		}
	}
	
	
	@RequestMapping("/updateUser")
	@ResponseBody
	public ResultMsg updateUser(HttpServletRequest request,HttpServletResponse response,TopUser user) throws Exception{
		try{
			userService.updateUser(user);
			return new ResultMsg(ResultMsg.SUCCESS,"修改成功");
		}catch(Exception e){
			e.printStackTrace();
			return new ResultMsg(ResultMsg.FAIL,"修改失败");
		}
	}
	
	@RequestMapping("/getUserList")
	@ResponseBody
	public String getUserList(HttpServletRequest request,HttpServletResponse response,TopUser user,Integer pageNum,Integer pageSize) throws Exception{
		String result = userService.getUserList(pageNum,pageSize,user);
		return result;
	}
	
	@RequestMapping("/deleteUsers")
	@ResponseBody
	public ResultMsg deleteUsers(HttpServletRequest request,HttpServletResponse response,String userIds) throws Exception{
		try{
			userService.deleteUsers(userIds.split(","));
			return new ResultMsg(ResultMsg.SUCCESS,"删除成功");
		}catch(Exception e){
			e.printStackTrace();
			return new ResultMsg(ResultMsg.FAIL,"删除失败");
		}
	}
	
	@RequestMapping("/exportUsers")
	@ResponseBody
	public ResultMsg exportUsers(@RequestParam("file") MultipartFile file) throws Exception{
		try{
			String result = userService.exportUsers(file);
	        return new ResultMsg(ResultMsg.SUCCESS,"导入成功",result);
		}catch(Exception e){
			e.printStackTrace();
			return new ResultMsg(ResultMsg.FAIL,e.getMessage());
		}
	}
	
	@RequestMapping("/confirmTempUsers")
	@ResponseBody
	public ResultMsg confirmTempUsers(HttpServletRequest request,HttpServletResponse response,String userListString) throws Exception{
		try{
	        userService.confirmTempUsers(userListString);
	        return new ResultMsg(ResultMsg.SUCCESS,"确认成功");
		}catch(Exception e){
			e.printStackTrace();
			return new ResultMsg(ResultMsg.FAIL,e.getMessage());
		}
	}
	
	@RequestMapping("/downloadTemp")
	public void downloadTemp(HttpServletRequest request,HttpServletResponse response) throws Exception{
		 String path = PropUtil.get("download.url")+"UserTemp.xlsx";
		 File file = new File(path);
         // 取得文件名。
         String filename = file.getName();
         // 以流的形式下载文件。
         InputStream fis = new BufferedInputStream(new FileInputStream(path));
         byte[] buffer = new byte[fis.available()];
         fis.read(buffer);
         fis.close();
         // 清空response
         response.reset();
         // 设置response的Header
         response.addHeader("Content-Disposition", "attachment;filename=" + new String(filename.getBytes()));
         response.addHeader("Content-Length", "" + file.length());
         OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
         response.setContentType("application/octet-stream");
         toClient.write(buffer);
         toClient.flush();
         toClient.close();
	}
	
	@RequestMapping("/getExpertNamesById")
	@ResponseBody
	public ResultMsg getExpertNamesById(HttpServletRequest request,HttpServletResponse response,String expertIds) throws Exception{
		try{
	        String result = userService.getExpertNamesById(expertIds);
	        return new ResultMsg(ResultMsg.SUCCESS,result);
		}catch(Exception e){
			e.printStackTrace();
			return new ResultMsg(ResultMsg.FAIL,"读取数据超时");
		}
	}


}
